import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * Create by chenhd on 2021/8/28.
 * 方法十八：使用LockSupport
 */
public class ConcurrentTask18 {
    public String str;

    public static void main(String[] args) {
        ConcurrentTask18 concurrentTask = new ConcurrentTask18();
        Thread current = Thread.currentThread();
        new Thread(() -> concurrentTask.putStr(current)).start();
        System.out.println(concurrentTask.getStr());
    }

    public void putStr(Thread thread) {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            LockSupport.unpark(thread);
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() {
        LockSupport.park();
        return str;
    }
}
